package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.JDBC;

import javax.xml.transform.Result;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsImpl implements NewsDao {
//    @Override
//    public List<NewsModel> getNewsModelListFromHomePage() {
//        String sql = "select * from news where where_show=1";
//        ResultSet resultSet = JDBC.query(sql);
//        List<NewsModel> list = new ArrayList<>();
//        try {
//            while (resultSet.next()) {
//                NewsModel newsModel = new NewsModel();
//                newsModel.setTitle(resultSet.getString("title"));
//                list.add(newsModel);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return list;
//    }

    @Override
    public List<NewsModel> getNewsModelList(NewsModel model) {
        String sql="";
        if(model.getWhereShow()==1){
             sql = "select * from news where where_show=" + model.getWhereShow()+"" +
                     " order by id desc  limit 3 ";
        }else if(model.getWhereShow()==2){
             sql = "select * from news where where_show=" + model.getWhereShow()+" " +
                     " order by id desc";
            sql += " limit " + (model.getPage() - 1) * +model.getLimit() + ", " + model.getLimit() + ";";
        }
        else if(model.getWhereShow()==3){
//             sql = "select * from news where where_show=" + model.getWhereShow();
        }

        ResultSet resultSet = JDBC.query(sql);
        List<NewsModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                NewsModel newsModel = new NewsModel();
                newsModel.setTitle(resultSet.getString("title"));
                newsModel.setId(resultSet.getInt("id"));
                newsModel.setCreateTime(resultSet.getString("create_time"));
                newsModel.setContent(resultSet.getString("content"));
                list.add(newsModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int delete(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int add(NewsModel newsModel) {
        return 0;
    }

    @Override
    public NewsModel findById(NewsModel newsModel) {
        return null;
    }

    @Override
    public int update(NewsModel newsModel) {
        return 0;
    }

    @Override
    public List<NewsModel> findAll(NewsModel newsModel) {
        return null;
    }

    @Override
    public int getCount(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int updateEnable(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int updateField(NewsModel newsModel) {
        return 0;
    }
}
